home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / dev / gcc / ixemul_src.lha / ixemul-41.0 / library / __fselect.c < prev    next >
C/C++ Source or Header  |  1995-05-27  |  4KB  |  116 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  __fselect.c,v 1.1.1.1 1994/04/04 04:29:45 amiga Exp
  20.  *
  21.  *  __fselect.c,v
  22.  * Revision 1.1.1.1  1994/04/04  04:29:45  amiga
  23.  * Initial CVS check in.
  24.  *
  25.  *  Revision 1.2  1993/11/05  21:49:09  mw
  26.  *  socket-changes
  27.  *
  28.  *  Revision 1.1  1992/05/14  19:55:40  mwild
  29.  *  Initial revision
  30.  *
  31.  */
  32.  
  33. #define KERNEL
  34. #include "ixemul.h"
  35. #include "kprintf.h"
  36.  
  37. #include "select.h"
  38.  
  39. /* don't use the `normal' packet port for select. We need synchronous
  40.  * notification to be able to Wait() for multiple replies */
  41. #undef __rwport
  42. #define __rwport (u.u_sync_mp)
  43.  
  44. /*
  45.  * select operation on a "normal" AmigaDOS filehandle. Normal means,
  46.  * we only have the WaitForChar() call available to find out, whether
  47.  * a read would block. Write() cannot be caught, so we always allow it.
  48.  * Request for exceptional data is routed to read.
  49.  */
  50.  
  51. int
  52. __fselect (struct file *f, int select_cmd, int io_mode)
  53. {
  54.   int result;
  55.  
  56.   if (select_cmd == SELCMD_PREPARE)
  57.     {
  58.       /* always possible... perhaps return ~0 in this case ???? */
  59.       if (io_mode != SELMODE_IN) return 0;
  60.  
  61.       /* should not wait here, but I have to.. the only case that a packet
  62.        * could be outstanding, is if this is a file open for read and write,
  63.        * and an async write is outstanding. Since I'm testing for read I have
  64.        * to wait for write() to complete */
  65.       __get_file (f);
  66.       __wait_packet (&f->f_sp);
  67.       LastError(f) = 0;
  68.  
  69.       SendPacket1(f,__rwport,ACTION_WAIT_CHAR, SELTIMEOUT);
  70.       return 1 << __rwport->mp_SigBit;
  71.     }
  72.   else if (select_cmd == SELCMD_CHECK)
  73.     {
  74.       /* only read is supported, other modes default to `ok' */
  75.       if (io_mode != SELMODE_IN) return 1;
  76.  
  77.       __wait_sync_packet (&f->f_sp);
  78.       /* there are two possible answers: error (packet not supported) 
  79.        * and the `real' answer.
  80.        * An error is treated as to allow input, so select() won't block
  81.        * indefinitely...
  82.        * & 1 converts dos-true (-1) into normal true (1) ;-)
  83.        */
  84.       result = LastError(f) ? 1 : (LastResult(f) & 1);
  85.       /* don't make __write() think its last packet failed.. */
  86.       LastError(f) = 0;
  87.       __release_file (f);
  88.       return result;
  89.     }
  90.   else if (select_cmd == SELCMD_POLL)
  91.     {
  92.       /* only read is supported, other modes default to `ok' */
  93.       if (io_mode != SELMODE_IN) return 1;
  94.  
  95.       __get_file (f);
  96.       __wait_packet (&f->f_sp);
  97.  
  98.       LastError(f) = 0;
  99.       SendPacket1(f,__rwport,ACTION_WAIT_CHAR, 0);
  100.       __wait_sync_packet (&f->f_sp);
  101.       /* there are two possible answers: error (packet not supported) 
  102.        * and the `real' answer.
  103.        * An error is treated as to allow input, so select() won't block
  104.        * indefinitely...
  105.        * & 1 converts dos-true (-1) into normal true (1) ;-)
  106.        */
  107.       result = LastError(f) ? 1 : (LastResult(f) & 1);
  108.       /* don't make __write() think its last packet failed.. */
  109.       LastError(f) = 0;
  110.       __release_file (f);
  111.       return result;
  112.     }
  113.   else
  114.     return 0;
  115. }
  116.